package com.spzx.order.controller;

import java.util.List;
import java.util.Arrays;

import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.order.domain.OrderForm;
import com.spzx.order.domain.TradeVo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.order.api.domain.OrderInfo;
import com.spzx.order.service.IOrderInfoService;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import io.swagger.v3.oas.annotations.Operation;
import com.spzx.common.core.web.page.TableDataInfo;

/**
 * 订单Controller
 *
 * @author atguigu
 * @date 2024-11-01
 */
@RestController
@RequestMapping("/orderInfo")
public class OrderInfoController extends BaseController
{
    @Resource
    private IOrderInfoService orderInfoService;

    @GetMapping("/getOrderByOrderNo/{orderNo}")
    public R<OrderInfo> getOrderByOrderNo(@PathVariable("orderNo") String orderNo){
        OrderInfo orderInfo = orderInfoService.getOrderByOrderNo(orderNo);
        return R.ok(orderInfo);
    }

    @GetMapping("/getOrderInfo/{orderId}")
    public AjaxResult getOrderInfo(@PathVariable Long orderId){
        return success(orderInfoService.getById(orderId));
    }


    // 根据接口说明文档开发
    @PostMapping("/submitOrder")
    public AjaxResult submitOrder(@RequestBody OrderForm orderForm){
        Long orderId = orderInfoService.submitOrder(orderForm);
        return success(orderId);
    }

    @GetMapping("trade")
    public AjaxResult trade(){
        TradeVo tradeVo = orderInfoService.trade();
        return success(tradeVo);
    }

    /**
     * 查询订单列表
     */
    @Operation(summary = "查询订单列表")
    @GetMapping("/list")
    public TableDataInfo list(OrderInfo orderInfo)
    {
        startPage();
        List<OrderInfo> list = orderInfoService.selectOrderInfoList(orderInfo);
        return getDataTable(list);
    }


    /**
     * 获取订单详细信息
     */
    @Operation(summary = "获取订单详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(orderInfoService.getById(id));
    }

    /**
     * 新增订单
     */
    @Operation(summary = "新增订单")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderInfo orderInfo)
    {
        return toAjax(orderInfoService.save(orderInfo));
    }

    /**
     * 修改订单
     */
    @Operation(summary = "修改订单")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderInfo orderInfo)
    {
        return toAjax(orderInfoService.updateById(orderInfo));
    }

    /**
     * 删除订单
     */
    @Operation(summary = "删除订单")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(orderInfoService.removeBatchByIds(Arrays.asList(ids)));
    }
}
